
// 创建一个SegmentView实例
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var http = require("helper/http");
var httpSilence = require("helper/httpSilence");
var circleUrl = require("url/circle");
var circleHelper = require("helper/circle");

var listdata = mm("do_ListData");
var communityUtil = require("helper/community");
var listview = ui("listView");
listview.bindItems(listdata);

var noData = [{"template":2,"labelContent":"这个社区还没有圈子!"}];


//本页面-ajax业务请求url
var httpUrl = circleUrl.url.base.list4Community;//社区圈子
var pageNum=1;

//UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
var listView = ui("listView");

//andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
//左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
//右上角-
var headerRight = ui(addheader + ".headerRight");
headerRight.visible = false;
headerLeft.on("touch", function() {
	do_App.closePage()
});
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "圈子",
})
header.refreshData();
//页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	binddata();
});
//全部刷新数据
function binddata() {
	var hasCircle = false;
	pageNum = 1;
	var params = {page:pageNum,communityId : communityUtil.communityVisitId()}
	http.post(httpUrl, params, function(data) {
		listview.rebound();
		listdata.removeAll();// 清除数据
		var myJionCircles = data.myJionCircles;
		if(myJionCircles){
			if(myJionCircles.length>0){
				hasCircle = true;
				listdata.addOne({"template":1,"title":"我加入的圈子"});
				listdata.addData(myJionCircles);
				listview.refreshItems();// 刷新item数据
				setNewTip4CircleBath(myJionCircles);
			}
		}
		if (data.page.content) {
			var circleList = data.page.content;
			if(circleList.length > 0){
				hasCircle = true;
				listdata.addOne({"template":1,"title":"社区其他圈子"});
				listdata.addData(circleList);
			}
		}
		if(!hasCircle){
			listdata.addData(noData);
		}
		listview.refreshItems();// 刷新item数据
		
	});
}
//刷新数据
function getNextPageData() {
	pageNum++;
	var params = {page:pageNum,communityId : communityUtil.communityVisitId()}
	http.post(httpUrl, params, function(data) {
		if (data.page.content) {
			var circleList = data.page.content;
			listview.rebound();
			if(circleList.length > 0){
				listdata.addData(circleList);
			}
			listview.refreshItems();// 刷新item数据
		}
	});
}

//下拉列表，刷新数据
listview.on("pull", function(data) {
	if (data.state == 2) {
		binddata();
		listview.rebound();//复位【一定要做】
	}
});

// 上拉列表，翻页数据
listview.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
		listview.rebound();//复位【一定要做】
	}
});

//查询我的圈子的话题红点
function setNewTip4CircleBath(vsMyCircles) {
	for (var i = 0; i < vsMyCircles.length; i++) {
		var oneData = vsMyCircles[i];
		var vsCircleId  = oneData.id;
		var lastSeenTime = circleHelper.cacheLastTime4Circle(vsCircleId);
		var params = {circleId: vsCircleId,startAt: lastSeenTime};
		httpSilence.post(circleUrl.url.topic.newCount4Circle,params,function(data){
			setNew4OneCircle(data.circleId ,data.newTopicCount);
		});
	}
}

function setNew4OneCircle(circleId , newCount) {
	for (var i = 0; i < listdata.getCount(); i++) {
		var oneData = listdata.getOne(i);
		if(oneData.id == circleId){
			oneData.newTopicCount = newCount;
			listdata.updateOne(i, oneData);
			listview.refreshItems();
		}
	}
}
